import java.util.Arrays;

public class AmoebaCode {
	public int find(String code, int K) {
		int length = code.length();
		int maxMask = 1;
		for (int i = 0; i < K; i++)
			maxMask *= K + 1;
		int[][] maximalDistance = new int[length + 1][maxMask];
		for (int[] row : maximalDistance)
			Arrays.fill(row, -1);
		int[] lastPosition = new int[K + 1];
		Arrays.fill(lastPosition, -K);
		return findMaximalDistance(maximalDistance, lastPosition, 0, 0, code.toCharArray());
	}

	private int findMaximalDistance(int[][] maximalDistance, int[] lastPosition, int position, int mask, char[] word) {
		if (maximalDistance[position][mask] != -1)
			return maximalDistance[position][mask];
		int K1 = lastPosition.length;
		if (position == maximalDistance.length - 1)
			return maximalDistance[position][mask] = K1 - 1;
		int result = 0;
		for (int i = 1; i < K1; i++) {
			if (word[position] == '0' || word[position] == i + '0') {
				int oldLastPosition = lastPosition[i];
				lastPosition[i] = position;
				result = Math.max(result, Math.min(findMaximalDistance(maximalDistance, lastPosition, position + 1,
						(mask * K1 + i) % maximalDistance[position].length, word), position - oldLastPosition));
				lastPosition[i] = oldLastPosition;
			}
		}
		return maximalDistance[position][mask] = result;
	}


	// BEGIN CUT HERE
	public static void main(String[] args) {
		if (args.length == 0) {
			AmoebaCodeHarness.run_test(-1);
		} else {
			for (int i=0; i<args.length; ++i)
				AmoebaCodeHarness.run_test(Integer.valueOf(args[i]));
		}
	}
// END CUT HERE
}

// BEGIN CUT HERE
class AmoebaCodeHarness {
	public static void run_test(int casenum) {
		if (casenum != -1) {
			if (runTestCase(casenum) == -1)
				System.err.println("Illegal input! Test case " + casenum + " does not exist.");
			return;
		}
		
		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = runTestCase(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}
		
		if (total == 0) {
			System.err.println("No test cases run.");
		} else if (correct < total) {
			System.err.println("Some cases FAILED (passed " + correct + " of " + total + ").");
		} else {
			System.err.println("All " + total + " tests passed!");
		}
	}
	
	static boolean compareOutput(int expected, int result) { return expected == result; }
	static String formatResult(int res) {
		return String.format("%d", res);
	}
	
	static int verifyCase(int casenum, int expected, int received) { 
		System.err.print("Example " + casenum + "... ");
		if (compareOutput(expected, received)) {
			System.err.println("PASSED");
			return 1;
		} else {
			System.err.println("FAILED");
			System.err.println("    Expected: " + formatResult(expected)); 
			System.err.println("    Received: " + formatResult(received)); 
			return 0;
		}
	}

	static int runTestCase(int casenum) {
		switch(casenum) {
		case 0: {
			String code               = "01";
			int K                     = 1;
			int expected__            = 1;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}
		case 1: {
			String code               = "1001";
			int K                     = 2;
			int expected__            = 1;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}
		case 2: {
			String code               = "1010";
			int K                     = 2;
			int expected__            = 2;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}
		case 3: {
			String code               = "01001";
			int K                     = 3;
			int expected__            = 3;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}
		case 4: {
			String code               = "10012031001";
			int K                     = 3;
			int expected__            = 2;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}

		// custom cases

/*      case 5: {
			String code               = ;
			int K                     = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}*/
/*      case 6: {
			String code               = ;
			int K                     = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}*/
/*      case 7: {
			String code               = ;
			int K                     = ;
			int expected__            = ;

			return verifyCase(casenum, expected__, new AmoebaCode().find(code, K));
		}*/
		default:
			return -1;
		}
	}
}

// END CUT HERE
